package com.four.project.controller;

import com.four.project.pojo.User;
import com.four.project.service.UserService;
import com.four.project.vo.UserReq;
import com.four.project.vo.UserResp;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @Author 梯子君(xie)
 * @Data 2020/7/615:46
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserService userService;

    //查询所有
    @RequestMapping(value = "selectAll" ,method = RequestMethod.POST)
    @ResponseBody
    public UserResp selectAll(@Param("page") Integer page ,@Param("size") Integer size){
        PageInfo pageInfo = userService.selectAll(page, size);
        long total = pageInfo.getTotal();
        List<User> list = pageInfo.getList();
        UserResp userResp = new UserResp();
        userResp.setUsers(list);
        userResp.setTotal((int) total);
        return userResp;
    }
    //通过id查询一个
    @RequestMapping(value = "selectById" , method = RequestMethod.POST)
    @ResponseBody
    public User selectById(Integer id){
        User user = userService.selectById(id);
        return user;
    }
    //添加一个
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public int addOne(User user){
//        user.setState("0");
        int i = userService.addOne(user);
        return i;
    }
    //通过id删除user
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public int delete(Integer id){
        int i = userService.deleteById(id);
        return i;
    }
    //通过id删除user
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public int update(User user){
        int i = userService.updateOne(user);
        return i;
    }

    /**
     * 发送验证码
     * @param email
     * @param request
     * @return
     */
    @RequestMapping(value = "/sendMail",method = RequestMethod.POST)
    @ResponseBody
    public  String sendMail(@RequestParam String email,HttpServletRequest request){
        try {
            userService.sendMail(email,request);
            return "success";
        }catch (Exception e){
            return "fail";
        }

    }


    /**
     *  用户注册
     * @param userReq
     * @param request
     * @return
     */
    @RequestMapping(value = "/registory",method = RequestMethod.POST)
    @ResponseBody
    public String registory(@RequestParam("user") UserReq userReq, HttpServletRequest request){
        String code = userReq.getCode();//用户输入的验证码
        String attribute =(String) request.getAttribute(userReq.getEmail());//服务器存储的yzm
        if (code.equals(attribute)){
            return "fail";//验证码错误
        }
        User user = new User();
        user.setEmail(userReq.getEmail());
        user.setUsername(userReq.getUsername());
        user.setPassword(userReq.getPassword());
        user.setState("1");
        int i = userService.addOne(user);
        if(i==1){
            return "success";
        }else{
            return "fail";
        }
    }

    /**
     * 用户登录
     * @param userReq email  password  封装在内
     * @param request
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(UserReq userReq,HttpServletRequest request){

        //获取email和password
        String imputemail = userReq.getEmail();
        String imputpassword = userReq.getPassword();
        //创建一个新token并将邮箱和密码置入
        UsernamePasswordToken token = new UsernamePasswordToken(imputemail, imputpassword);
        //获取shiro的主体
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            if (subject.isAuthenticated()){
                return "success";
            }else{
                return "fail";
            }
        }catch (Exception  e){

        }






        int login = userService.login(userReq, request);
        if (login==0){
            return "fail";
        }
        return "success";
    }















}
